package com.example.service;

import com.example.entity.Scientific;
import com.example.entity.Zongxiang;
import com.example.mapper.ScientificMapper;
import com.example.mapper.ZongxiangMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ZongxiangService {

    @Resource
    private ZongxiangMapper ZongxiangMapper;

    // 添加新的纵向课题记录
    public String add(Zongxiang zongxiang) {
        try {
            ZongxiangMapper.insert(zongxiang);
        } catch (DuplicateKeyException e) {
            return "重复！";
        }
        return null;
    }

    // 根据ID更新纵向课题记录
    public void updateById(Zongxiang zongxiang) {
        ZongxiangMapper.updateById(zongxiang);
    }

    // 根据ID获取纵向课题记录
    public Zongxiang getById(Integer id) {
        return ZongxiangMapper.selectById(id);
    }

    // 根据ID删除纵向课题记录
    public void deleteById(Integer id) {
        ZongxiangMapper.deleteById(id);
    }

    // 分页查询纵向课题记录
    public PageInfo<Zongxiang> selectPage(Integer pageNum, Integer pageSize, Zongxiang zongxiang) {
        PageHelper.startPage(pageNum, pageSize);
        List<Zongxiang> zongxiangs = ZongxiangMapper.selectAll(zongxiang);
        return PageInfo.of(zongxiangs);
    }
}
